class Gameone extends eui.Component implements eui.UIComponent {
	public constructor() {
		super();
		this.skinName = "GameoneSkin";
		this.addEventListener(egret.Event.ADDED_TO_STAGE, function () {
			this.kaiguan = true;
			let that = this;
			//noBorder模式
			this.winW = window.innerWidth;
			this.winH = window.innerHeight;
			let winR = this.winW / this.winH;
			this.stageW = this.stage.stageWidth;
			this.stageH = this.stage.stageHeight;
			let stageR: number = this.stageW / this.stageH;

			if (stageR > winR) {//裁剪舞台的宽
				this.plane.y = this.stageH - this.plane.height - 150;
				this.left.y = this.stageH - this.left.height - 34;
				this.right.y = this.stageH - this.right.height - 34;
			} else {//裁剪舞台高
				let ration = this.stageW / this.winW;
				let finStageH = this.stageH / ration;
				let ckipH = (finStageH - this.winH) / 2;
				this.gameoneBgg.y = this.stageH - ckipH * ration - this.gameoneBgg.height;
				this.plane.y = this.stageH - ckipH * ration - this.plane.height - 130;
				this.leftBtn.y = this.stageH - ckipH * ration - this.leftBtn.height - 20;
				this.rightBtn.y = this.stageH - ckipH * ration - this.rightBtn.height - 20;
			}
			//掉下石头
			this.pictureArr = ["game1_png", "game2_png", "game3_png", "game4_png", "game5_png"]
			this.LV1 = setInterval(() => {
				this.index = Math.random() * this.pictureArr.length | 0;
				let shitou = new Stone(this.pictureArr[this.index], 4000);
				if(this.index==0){
					shitou.score = 100;
				}else{
					shitou.score = -100;
				}
				
				this.shitouArr.addChild(shitou);
				setTimeout(() => {
					this.shitouArr.removeChild(shitou);
					this.kaiguan = true;
				}, 6000)
				//为了做像素碰撞检测，必须时时刻刻获取飞机的中心点,那就需要每一帧都监听，即需要一个计时器
				shitou.addEventListener(egret.Event.ENTER_FRAME, this.liyuan, this)
			}, 1000)
		}, this)
		this.leftBtn.addEventListener(egret.TouchEvent.TOUCH_BEGIN, function () {
			egret.startTick(this.beginL, this)
		}, this)
		this.leftBtn.addEventListener(egret.TouchEvent.TOUCH_END, function () {
			egret.stopTick(this.beginL, this)
		}, this)
		this.leftBtn.addEventListener(egret.TouchEvent.TOUCH_RELEASE_OUTSIDE, function () {
			egret.stopTick(this.beginL, this)
		}, this)
		this.rightBtn.addEventListener(egret.TouchEvent.TOUCH_BEGIN, function () {
			egret.startTick(this.beginR, this)
		}, this)
		this.rightBtn.addEventListener(egret.TouchEvent.TOUCH_END, function () {
			egret.stopTick(this.beginR, this)
		}, this)
		this.rightBtn.addEventListener(egret.TouchEvent.TOUCH_RELEASE_OUTSIDE, function () {
			egret.stopTick(this.beginR, this)
		}, this)
	}
	public liyuan(e) {
		//1s触发30次  index里面的帧
		this.fenshu.text = this.fenshuText.toString();
		let x = this.plane.x + this.plane.width / 2;
		let y = this.plane.y + this.plane.height / 2;
		var isHit: boolean = e.target.hitTestPoint(x, y);

		console.log(e.target.score);
		if (isHit) {
			e.target.scaleX = 0;
			if(this.kaiguan){
				this.fenshuText += e.target.score;
				this.kaiguan = false
			}
		}
		if (this.fenshuText >= 100) {
			e.target.removeEventListener(egret.Event.ENTER_FRAME, this.liyuan, this);
			clearInterval(this.LV1)
			SceneManger.createObj(this.stage).loadScene("gametwo", Gametwo);
		}
	}
	protected partAdded(partName: string, instance: any): void {
		super.partAdded(partName, instance);
	}
	//获取部件的方法：声明一个和部件同ID名的属性
	public gameoneBgg;
	private fenshu;
	public plane;
	public leftBtn;
	public rightBtn;
	public stageH;
	public winW;
	public winH;
	public stageW;
	private shitouArr;
	private fenshuText = 0;
	private kaiguan: boolean;
	private pictureArr;
	private index;
	private LV1;
	private beginL() {
		if (this.plane.x <= 0) {
			this.plane.x = 0;
		} else {
			this.plane.x -= 6;
		}
	}
	private beginR() {
		if (this.plane.x > this.stage.stageWidth - this.plane.width) {
			this.plane.x = this.stage.stageWidth - this.plane.width
		} else {
			this.plane.x += 6;
		}
	}
	protected childrenCreated(): void {
		super.childrenCreated();

	}


}